gecko-dev/third_party/rust/regex-0.2.2
Bastien Orivel 15328d6b4f Bug 1463251 - Part 2. Revendor dependencies. r=ato,jgraham
MozReview-Commit-ID: 6eeqvTqSxam

--HG--
rename : third_party/rust/memchr/.cargo-checksum.json => third_party/rust/memchr-1.0.2/.cargo-checksum.json
rename : third_party/rust/memchr/Cargo.toml => third_party/rust/memchr-1.0.2/Cargo.toml
rename : third_party/rust/memchr/src/lib.rs => third_party/rust/memchr-1.0.2/src/lib.rs
rename : third_party/rust/regex/.cargo-checksum.json => third_party/rust/regex-0.2.2/.cargo-checksum.json
rename : third_party/rust/regex/.travis.yml => third_party/rust/regex-0.2.2/.travis.yml
rename : third_party/rust/regex/CHANGELOG.md => third_party/rust/regex-0.2.2/CHANGELOG.md
rename : third_party/rust/regex/Cargo.toml => third_party/rust/regex-0.2.2/Cargo.toml
rename : third_party/rust/regex/HACKING.md => third_party/rust/regex-0.2.2/HACKING.md
rename : third_party/rust/regex/PERFORMANCE.md => third_party/rust/regex-0.2.2/PERFORMANCE.md
rename : third_party/rust/regex/README.md => third_party/rust/regex-0.2.2/README.md
rename : third_party/rust/regex/appveyor.yml => third_party/rust/regex-0.2.2/appveyor.yml
rename : third_party/rust/regex/ci/after_success.sh => third_party/rust/regex-0.2.2/ci/after_success.sh
rename : third_party/rust/regex/ci/run-kcov => third_party/rust/regex-0.2.2/ci/run-kcov
rename : third_party/rust/regex/ci/script.sh => third_party/rust/regex-0.2.2/ci/script.sh
rename : third_party/rust/regex/examples/bug347.rs => third_party/rust/regex-0.2.2/examples/bug347.rs
rename : third_party/rust/regex/examples/shootout-regex-dna-bytes.rs => third_party/rust/regex-0.2.2/examples/shootout-regex-dna-bytes.rs
rename : third_party/rust/regex/examples/shootout-regex-dna-single.rs => third_party/rust/regex-0.2.2/examples/shootout-regex-dna-single.rs
rename : third_party/rust/regex/examples/shootout-regex-dna.rs => third_party/rust/regex-0.2.2/examples/shootout-regex-dna.rs
rename : third_party/rust/regex/examples/shootout-regex-redux-1.rs => third_party/rust/regex-0.2.2/examples/shootout-regex-redux-1.rs
rename : third_party/rust/regex/examples/shootout-regex-redux-chunked.rs => third_party/rust/regex-0.2.2/examples/shootout-regex-redux-chunked.rs
rename : third_party/rust/regex/examples/shootout-regex-redux.rs => third_party/rust/regex-0.2.2/examples/shootout-regex-redux.rs
rename : third_party/rust/regex/scripts/unicode.py => third_party/rust/regex-0.2.2/scripts/unicode.py
rename : third_party/rust/regex/src/backtrack.rs => third_party/rust/regex-0.2.2/src/backtrack.rs
rename : third_party/rust/regex/src/compile.rs => third_party/rust/regex-0.2.2/src/compile.rs
rename : third_party/rust/regex/src/dfa.rs => third_party/rust/regex-0.2.2/src/dfa.rs
rename : third_party/rust/regex/src/error.rs => third_party/rust/regex-0.2.2/src/error.rs
rename : third_party/rust/regex/src/exec.rs => third_party/rust/regex-0.2.2/src/exec.rs
rename : third_party/rust/regex/src/expand.rs => third_party/rust/regex-0.2.2/src/expand.rs
rename : third_party/rust/regex/src/input.rs => third_party/rust/regex-0.2.2/src/input.rs
rename : third_party/rust/regex/src/lib.rs => third_party/rust/regex-0.2.2/src/lib.rs
rename : third_party/rust/regex/src/literals.rs => third_party/rust/regex-0.2.2/src/literals.rs
rename : third_party/rust/regex/src/pikevm.rs => third_party/rust/regex-0.2.2/src/pikevm.rs
rename : third_party/rust/regex/src/prog.rs => third_party/rust/regex-0.2.2/src/prog.rs
rename : third_party/rust/regex/src/re_builder.rs => third_party/rust/regex-0.2.2/src/re_builder.rs
rename : third_party/rust/regex/src/re_bytes.rs => third_party/rust/regex-0.2.2/src/re_bytes.rs
rename : third_party/rust/regex/src/re_plugin.rs => third_party/rust/regex-0.2.2/src/re_plugin.rs
rename : third_party/rust/regex/src/re_set.rs => third_party/rust/regex-0.2.2/src/re_set.rs
rename : third_party/rust/regex/src/re_trait.rs => third_party/rust/regex-0.2.2/src/re_trait.rs
rename : third_party/rust/regex/src/re_unicode.rs => third_party/rust/regex-0.2.2/src/re_unicode.rs
rename : third_party/rust/regex/src/simd_accel/mod.rs => third_party/rust/regex-0.2.2/src/simd_accel/mod.rs
rename : third_party/rust/regex/src/simd_accel/teddy128.rs => third_party/rust/regex-0.2.2/src/simd_accel/teddy128.rs
rename : third_party/rust/regex/src/simd_fallback/mod.rs => third_party/rust/regex-0.2.2/src/simd_fallback/mod.rs
rename : third_party/rust/regex/src/simd_fallback/teddy128.rs => third_party/rust/regex-0.2.2/src/simd_fallback/teddy128.rs
rename : third_party/rust/regex/src/utf8.rs => third_party/rust/regex-0.2.2/src/utf8.rs
rename : third_party/rust/regex/tests/api_str.rs => third_party/rust/regex-0.2.2/tests/api_str.rs
rename : third_party/rust/regex/tests/bytes.rs => third_party/rust/regex-0.2.2/tests/bytes.rs
rename : third_party/rust/regex/tests/crazy.rs => third_party/rust/regex-0.2.2/tests/crazy.rs
rename : third_party/rust/regex/tests/macros.rs => third_party/rust/regex-0.2.2/tests/macros.rs
rename : third_party/rust/regex/tests/macros_bytes.rs => third_party/rust/regex-0.2.2/tests/macros_bytes.rs
rename : third_party/rust/regex/tests/macros_str.rs => third_party/rust/regex-0.2.2/tests/macros_str.rs
rename : third_party/rust/regex/tests/noparse.rs => third_party/rust/regex-0.2.2/tests/noparse.rs
rename : third_party/rust/regex/tests/plugin.rs => third_party/rust/regex-0.2.2/tests/plugin.rs
rename : third_party/rust/regex/tests/regression.rs => third_party/rust/regex-0.2.2/tests/regression.rs
rename : third_party/rust/regex/tests/replace.rs => third_party/rust/regex-0.2.2/tests/replace.rs
rename : third_party/rust/regex/tests/test_default.rs => third_party/rust/regex-0.2.2/tests/test_default.rs
rename : third_party/rust/regex/tests/test_default_bytes.rs => third_party/rust/regex-0.2.2/tests/test_default_bytes.rs
rename : third_party/rust/regex/tests/test_plugin.rs => third_party/rust/regex-0.2.2/tests/test_plugin.rs
rename : third_party/rust/regex/tests/unicode.rs => third_party/rust/regex-0.2.2/tests/unicode.rs
rename : third_party/rust/regex/tests/word_boundary_unicode.rs => third_party/rust/regex-0.2.2/tests/word_boundary_unicode.rs
rename : third_party/rust/regex-syntax/.cargo-checksum.json => third_party/rust/regex-syntax-0.4.1/.cargo-checksum.json
rename : third_party/rust/regex-syntax/Cargo.toml => third_party/rust/regex-syntax-0.4.1/Cargo.toml
rename : third_party/rust/regex-syntax/src/lib.rs => third_party/rust/regex-syntax-0.4.1/src/lib.rs
rename : third_party/rust/regex-syntax/src/literals.rs => third_party/rust/regex-syntax-0.4.1/src/literals.rs
rename : third_party/rust/regex-syntax/src/parser.rs => third_party/rust/regex-syntax-0.4.1/src/parser.rs
rename : third_party/rust/regex-syntax/src/properties.rs => third_party/rust/regex-syntax-0.4.1/src/properties.rs
rename : third_party/rust/regex-syntax/src/unicode.rs => third_party/rust/regex-syntax-0.4.1/src/unicode.rs
extra : rebase_source : b0c643eb68b9262945345145ec1578c14369dbf7
2018-05-21 22:34:18 +02:00
..
ci
examples
scripts
src
tests
.cargo-checksum.json
.travis.yml
CHANGELOG.md
Cargo.toml
HACKING.md
LICENSE-APACHE
LICENSE-MIT
PERFORMANCE.md
README.md
appveyor.yml

README.md

regex

A Rust library for parsing, compiling, and executing regular expressions. Its syntax is similar to Perl-style regular expressions, but lacks a few features like look around and backreferences. In exchange, all searches execute in linear time with respect to the size of the regular expression and search text. Much of the syntax and implementation is inspired by RE2.

Build Status Build status Coverage Status

Documentation

Module documentation with examples. The module documentation also include a comprehensive description of the syntax supported.

Documentation with examples for the various matching functions and iterators can be found on the Regex type.

Usage

Add this to your Cargo.toml:

[dependencies]
regex = "0.2"

and this to your crate root:

extern crate regex;

Here's a simple example that matches a date in YYYY-MM-DD format and prints the year, month and day:

extern crate regex;

use regex::Regex;

fn main() {
    let re = Regex::new(r"(?x)
(?P<year>\d{4})  # the year
-
(?P<month>\d{2}) # the month
-
(?P<day>\d{2})   # the day
").unwrap();
    let caps = re.captures("2010-03-14").unwrap();

    assert_eq!("2010", &caps["year"]);
    assert_eq!("03", &caps["month"]);
    assert_eq!("14", &caps["day"]);
}

If you have lots of dates in text that you'd like to iterate over, then it's easy to adapt the above example with an iterator:

extern crate regex;

use regex::Regex;

const TO_SEARCH: &'static str = "
On 2010-03-14, foo happened. On 2014-10-14, bar happened.
";

fn main() {
    let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();

    for caps in re.captures_iter(TO_SEARCH) {
        // Note that all of the unwraps are actually OK for this regex
        // because the only way for the regex to match is if all of the
        // capture groups match. This is not true in general though!
        println!("year: {}, month: {}, day: {}",
                 caps.get(1).unwrap().as_str(),
                 caps.get(2).unwrap().as_str(),
                 caps.get(3).unwrap().as_str());
    }
}

This example outputs:

year: 2010, month: 03, day: 14
year: 2014, month: 10, day: 14

Usage: Avoid compiling the same regex in a loop

It is an anti-pattern to compile the same regular expression in a loop since compilation is typically expensive. (It takes anywhere from a few microseconds to a few milliseconds depending on the size of the regex.) Not only is compilation itself expensive, but this also prevents optimizations that reuse allocations internally to the matching engines.

In Rust, it can sometimes be a pain to pass regular expressions around if they're used from inside a helper function. Instead, we recommend using the lazy_static crate to ensure that regular expressions are compiled exactly once.

For example:

#[macro_use] extern crate lazy_static;
extern crate regex;

use regex::Regex;

fn some_helper_function(text: &str) -> bool {
    lazy_static! {
        static ref RE: Regex = Regex::new("...").unwrap();
    }
    RE.is_match(text)
}

Specifically, in this example, the regex will be compiled when it is used for the first time. On subsequent uses, it will reuse the previous compilation.

Usage: match regular expressions on &[u8]

The main API of this crate (regex::Regex) requires the caller to pass a &str for searching. In Rust, an &str is required to be valid UTF-8, which means the main API can't be used for searching arbitrary bytes.

To match on arbitrary bytes, use the regex::bytes::Regex API. The API is identical to the main API, except that it takes an &[u8] to search on instead of an &str. By default, . will match any byte using regex::bytes::Regex, while . will match any UTF-8 encoded Unicode scalar value using the main API.

This example shows how to find all null-terminated strings in a slice of bytes:

use regex::bytes::Regex;

let re = Regex::new(r"(?P<cstr>[^\x00]+)\x00").unwrap();
let text = b"foo\x00bar\x00baz\x00";

// Extract all of the strings without the null terminator from each match.
// The unwrap is OK here since a match requires the `cstr` capture to match.
let cstrs: Vec<&[u8]> =
    re.captures_iter(text)
      .map(|c| c.name("cstr").unwrap().as_bytes())
      .collect();
assert_eq!(vec![&b"foo"[..], &b"bar"[..], &b"baz"[..]], cstrs);

Notice here that the [^\x00]+ will match any byte except for NUL. When using the main API, [^\x00]+ would instead match any valid UTF-8 sequence except for NUL.

Usage: match multiple regular expressions simultaneously

This demonstrates how to use a RegexSet to match multiple (possibly overlapping) regular expressions in a single scan of the search text:

use regex::RegexSet;

let set = RegexSet::new(&[
    r"\w+",
    r"\d+",
    r"\pL+",
    r"foo",
    r"bar",
    r"barfoo",
    r"foobar",
]).unwrap();

// Iterate over and collect all of the matches.
let matches: Vec<_> = set.matches("foobar").into_iter().collect();
assert_eq!(matches, vec![0, 2, 3, 4, 6]);

// You can also test whether a particular regex matched:
let matches = set.matches("foobar");
assert!(!matches.matched(5));
assert!(matches.matched(6));

Usage: regex! compiler plugin

WARNING: The regex! compiler plugin is orders of magnitude slower than the normal Regex::new(...) usage. You should not use the compiler plugin unless you have a very special reason for doing so. The performance difference may be the temporary, but the path forward at this point isn't clear.

The regex! compiler plugin will compile your regexes at compile time. This only works with a nightly compiler.

Here is a small example:

#![feature(plugin)]

#![plugin(regex_macros)]
extern crate regex;

fn main() {
    let re = regex!(r"(\d{4})-(\d{2})-(\d{2})");
    let caps = re.captures("2010-03-14").unwrap();

    assert_eq!("2010", caps[1]);
    assert_eq!("03", caps[2]);
    assert_eq!("14", caps[3]);
}

Notice that we never unwrap the result of regex!. This is because your program won't compile if the regex doesn't compile. (Try regex!("(").)

Usage: a regular expression parser

This repository contains a crate that provides a well tested regular expression parser and abstract syntax. It provides no facilities for compilation or execution. This may be useful if you're implementing your own regex engine or otherwise need to do analysis on the syntax of a regular expression. It is otherwise not recommended for general use.

Documentation for regex-syntax with examples.

License

regex is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.

See LICENSE-APACHE, and LICENSE-MIT for details.