use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::hash_map::Iter;
use std::fmt;
use std::ops::Index;
#[cfg(feature = "pattern")]
use std::str::pattern::{Pattern, Searcher, SearchStep};
use std::str::FromStr;
use program::{Program, MatchEngine};
use syntax;
const REPLACE_EXPAND: &'static str = r"(?x)
(?P<before>^|\b|[^$]) # Ignore `$$name`.
\$
(?P<name> # Match the actual capture name. Can be...
[0-9]+ # A sequence of digits (for indexed captures), or...
|
[_a-zA-Z][_0-9a-zA-Z]* # A name for named captures.
)
";
pub type CaptureIdxs = [Option<usize>];
pub fn quote(text: &str) -> String {
let mut quoted = String::with_capacity(text.len());
for c in text.chars() {
if syntax::is_punct(c) {
quoted.push('\\')
}
quoted.push(c);
}
quoted
}
pub fn is_match(regex: &str, text: &str) -> Result<bool, Error> {
Regex::new(regex).map(|r| r.is_match(text))
}
#[derive(Debug)]
pub enum Error {
Syntax(syntax::Error),
CompiledTooBig(usize),
#[doc(hidden)]
__Nonexhaustive,
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Syntax(ref err) => err.description(),
Error::CompiledTooBig(_) => "compiled program too big",
Error::__Nonexhaustive => unreachable!(),
}
}
fn cause(&self) -> Option<&::std::error::Error> {
match *self {
Error::Syntax(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Syntax(ref err) => err.fmt(f),
Error::CompiledTooBig(limit) => {
write!(f, "Compiled regex exceeds size limit of {} bytes.",
limit)
}
Error::__Nonexhaustive => unreachable!(),
}
}
}
impl From<syntax::Error> for Error {
fn from(err: syntax::Error) -> Error {
Error::Syntax(err)
}
}
#[derive(Clone)]
pub enum Regex {
#[doc(hidden)]
Dynamic(Program),
#[doc(hidden)]
Native(ExNative),
}
#[doc(hidden)]
pub struct ExNative {
#[doc(hidden)]
pub original: &'static str,
#[doc(hidden)]
pub names: &'static &'static [Option<&'static str>],
#[doc(hidden)]
pub prog: fn(&mut CaptureIdxs, &str, usize) -> bool,
}
impl Copy for ExNative {}
impl Clone for ExNative {
fn clone(&self) -> ExNative {
*self
}
}
impl fmt::Display for Regex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl fmt::Debug for Regex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl PartialEq for Regex {
fn eq(&self, other: &Regex) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for Regex {}
impl FromStr for Regex {
type Err = Error;
fn from_str(s: &str) -> Result<Regex, Error> {
Regex::new(s)
}
}
impl Regex {
pub fn new(re: &str) -> Result<Regex, Error> {
Regex::with_size_limit(10 * (1 << 20), re)
}
pub fn with_size_limit(size: usize, re: &str) -> Result<Regex, Error> {
Regex::with_engine(None, size, re)
}
#[doc(hidden)]
pub fn with_engine(
engine: Option<MatchEngine>,
size: usize,
re: &str,
) -> Result<Regex, Error> {
Program::new(engine, size, re).map(Regex::Dynamic)
}
pub fn is_match(&self, text: &str) -> bool {
exec(self, &mut [], text, 0)
}
pub fn find(&self, text: &str) -> Option<(usize, usize)> {
let mut caps = [None, None];
if exec(self, &mut caps, text, 0) {
Some((caps[0].unwrap(), caps[1].unwrap()))
} else {
None
}
}
pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindMatches<'r, 't> {
FindMatches {
re: self,
search: text,
last_end: 0,
last_match: None,
}
}
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>> {
let mut caps = self.alloc_captures();
if exec(self, &mut caps, text, 0) {
Some(Captures::new(self, text, caps))
} else {
None
}
}
pub fn captures_iter<'r, 't>(&'r self, text: &'t str)
-> FindCaptures<'r, 't> {
FindCaptures {
re: self,
search: text,
last_match: None,
last_end: 0,
}
}
pub fn split<'r, 't>(&'r self, text: &'t str) -> RegexSplits<'r, 't> {
RegexSplits {
finder: self.find_iter(text),
last: 0,
}
}
pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: usize)
-> RegexSplitsN<'r, 't> {
RegexSplitsN {
splits: self.split(text),
cur: 0,
limit: limit,
}
}
pub fn replace<R: Replacer>(&self, text: &str, rep: R) -> String {
self.replacen(text, 1, rep)
}
pub fn replace_all<R: Replacer>(&self, text: &str, rep: R) -> String {
self.replacen(text, 0, rep)
}
pub fn replacen<R: Replacer>
(&self, text: &str, limit: usize, mut rep: R) -> String {
let mut new = String::with_capacity(text.len());
let mut last_match = 0;
if rep.no_expand().is_some() {
let rep = rep.no_expand().unwrap();
for (i, (s, e)) in self.find_iter(text).enumerate() {
if limit > 0 && i >= limit {
break
}
new.push_str(&text[last_match..s]);
new.push_str(&rep);
last_match = e;
}
} else {
for (i, cap) in self.captures_iter(text).enumerate() {
if limit > 0 && i >= limit {
break
}
let (s, e) = cap.pos(0).unwrap();
new.push_str(&text[last_match..s]);
new.push_str(&rep.reg_replace(&cap));
last_match = e;
}
}
new.push_str(&text[last_match..]);
new
}
pub fn as_str<'a>(&'a self) -> &'a str {
match *self {
Regex::Dynamic(Program { ref original, .. }) => original,
Regex::Native(ExNative { ref original, .. }) => original,
}
}
pub fn capture_names<'r>(&'r self) -> CaptureNames<'r> {
match *self {
Regex::Native(ref n) => CaptureNames::Native(n.names.iter()),
Regex::Dynamic(ref d) => CaptureNames::Dynamic(d.cap_names.iter())
}
}
pub fn captures_len(&self) -> usize {
match *self {
Regex::Native(ref n) => n.names.len(),
Regex::Dynamic(ref d) => d.cap_names.len()
}
}
fn alloc_captures(&self) -> Vec<Option<usize>> {
match *self {
Regex::Native(ref n) => vec![None; 2 * n.names.len()],
Regex::Dynamic(ref d) => d.alloc_captures(),
}
}
}
pub enum CaptureNames<'r> {
#[doc(hidden)]
Native(::std::slice::Iter<'r, Option<&'static str>>),
#[doc(hidden)]
Dynamic(::std::slice::Iter<'r, Option<String>>)
}
impl<'r> Iterator for CaptureNames<'r> {
type Item=Option<&'r str>;
fn next(&mut self) -> Option<Option<&'r str>> {
match *self {
CaptureNames::Native(ref mut i) =>
i.next().map(|o| *o),
CaptureNames::Dynamic(ref mut i) =>
i.next().as_ref().map(|o| o.as_ref().map(|s| s.as_ref())),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match *self {
CaptureNames::Native(ref i) => i.size_hint(),
CaptureNames::Dynamic(ref i) => i.size_hint(),
}
}
}
pub struct NoExpand<'t>(pub &'t str);
pub trait Replacer {
fn reg_replace<'a>(&'a mut self, caps: &Captures) -> Cow<'a, str>;
fn no_expand<'a>(&'a mut self) -> Option<Cow<'a, str>> { None }
}
impl<'t> Replacer for NoExpand<'t> {
fn reg_replace<'a>(&'a mut self, _: &Captures) -> Cow<'a, str> {
self.0.into()
}
fn no_expand<'a>(&'a mut self) -> Option<Cow<'a, str>> {
Some(self.0.into())
}
}
impl<'t> Replacer for &'t str {
fn reg_replace<'a>(&'a mut self, caps: &Captures) -> Cow<'a, str> {
caps.expand(*self).into()
}
fn no_expand<'a>(&'a mut self) -> Option<Cow<'a, str>> {
match self.find('$') {
Some(_) => None,
None => Some((*self).into()),
}
}
}
impl<F> Replacer for F where F: FnMut(&Captures) -> String {
fn reg_replace<'a>(&'a mut self, caps: &Captures) -> Cow<'a, str> {
(*self)(caps).into()
}
}
pub struct RegexSplits<'r, 't> {
finder: FindMatches<'r, 't>,
last: usize,
}
impl<'r, 't> Iterator for RegexSplits<'r, 't> {
type Item = &'t str;
fn next(&mut self) -> Option<&'t str> {
let text = self.finder.search;
match self.finder.next() {
None => {
if self.last >= text.len() {
None
} else {
let s = &text[self.last..];
self.last = text.len();
Some(s)
}
}
Some((s, e)) => {
let matched = &text[self.last..s];
self.last = e;
Some(matched)
}
}
}
}
pub struct RegexSplitsN<'r, 't> {
splits: RegexSplits<'r, 't>,
cur: usize,
limit: usize,
}
impl<'r, 't> Iterator for RegexSplitsN<'r, 't> {
type Item = &'t str;
fn next(&mut self) -> Option<&'t str> {
let text = self.splits.finder.search;
if self.cur >= self.limit {
None
} else {
self.cur += 1;
if self.cur >= self.limit {
Some(&text[self.splits.last..])
} else {
self.splits.next()
}
}
}
}
pub struct Captures<'t> {
text: &'t str,
locs: Vec<Option<usize>>,
named: Option<HashMap<String, usize>>,
}
impl<'t> Captures<'t> {
fn new(
re: &Regex,
search: &'t str,
locs: Vec<Option<usize>>,
) -> Captures<'t> {
let named =
if re.captures_len() == 0 {
None
} else {
let mut named = HashMap::new();
for (i, name) in re.capture_names().enumerate() {
if let Some(name) = name {
named.insert(name.to_owned(), i);
}
}
Some(named)
};
Captures {
text: search,
locs: locs,
named: named,
}
}
pub fn pos(&self, i: usize) -> Option<(usize, usize)> {
let (s, e) = (i * 2, i * 2 + 1);
if e >= self.locs.len() || self.locs[s].is_none() {
return None
}
Some((self.locs[s].unwrap(), self.locs[e].unwrap()))
}
pub fn at(&self, i: usize) -> Option<&'t str> {
match self.pos(i) {
None => None,
Some((s, e)) => Some(&self.text[s..e])
}
}
pub fn name(&self, name: &str) -> Option<&'t str> {
match self.named {
None => None,
Some(ref h) => {
match h.get(name) {
None => None,
Some(i) => self.at(*i),
}
}
}
}
pub fn iter(&'t self) -> SubCaptures<'t> {
SubCaptures { idx: 0, caps: self, }
}
pub fn iter_pos(&'t self) -> SubCapturesPos<'t> {
SubCapturesPos { idx: 0, caps: self, }
}
pub fn iter_named(&'t self) -> SubCapturesNamed<'t> {
SubCapturesNamed { caps: self, inner: self.named.as_ref().map(|n| n.iter()) }
}
pub fn expand(&self, text: &str) -> String {
let re = Regex::new(REPLACE_EXPAND).unwrap();
let text = re.replace_all(text, |refs: &Captures| -> String {
let before = refs.name("before").unwrap_or("");
let name = refs.name("name").unwrap_or("");
format!("{}{}", before, match name.parse::<usize>() {
Err(_) => self.name(name).unwrap_or("").to_string(),
Ok(i) => self.at(i).unwrap_or("").to_string(),
})
});
let re = Regex::new(r"\$\$").unwrap();
re.replace_all(&text, NoExpand("$"))
}
#[inline]
pub fn len(&self) -> usize { self.locs.len() / 2 }
#[inline]
pub fn is_empty(&self) -> bool { self.len() == 0 }
}
impl<'t> Index<usize> for Captures<'t> {
type Output = str;
fn index<'a>(&'a self, i: usize) -> &'a str {
match self.at(i) {
None => panic!("no group at index '{}'", i),
Some(s) => s,
}
}
}
impl<'t> Index<&'t str> for Captures<'t> {
type Output = str;
fn index<'a>(&'a self, name: &str) -> &'a str {
match self.name(name) {
None => panic!("no group named '{}'", name),
Some(ref s) => s,
}
}
}
pub struct SubCaptures<'t> {
idx: usize,
caps: &'t Captures<'t>,
}
impl<'t> Iterator for SubCaptures<'t> {
type Item = Option<&'t str>;
fn next(&mut self) -> Option<Option<&'t str>> {
if self.idx < self.caps.len() {
self.idx += 1;
Some(self.caps.at(self.idx - 1))
} else {
None
}
}
}
pub struct SubCapturesPos<'t> {
idx: usize,
caps: &'t Captures<'t>,
}
impl<'t> Iterator for SubCapturesPos<'t> {
type Item = Option<(usize, usize)>;
fn next(&mut self) -> Option<Option<(usize, usize)>> {
if self.idx < self.caps.len() {
self.idx += 1;
Some(self.caps.pos(self.idx - 1))
} else {
None
}
}
}
pub struct SubCapturesNamed<'t>{
caps: &'t Captures<'t>,
inner: Option<Iter<'t, String, usize>>,
}
impl<'t> Iterator for SubCapturesNamed<'t> {
type Item = (&'t str, Option<&'t str>);
fn next(&mut self) -> Option<(&'t str, Option<&'t str>)> {
match self.inner.as_mut().map(|it| it.next()).unwrap_or(None) {
Some((name, pos)) => Some((name, self.caps.at(*pos))),
None => None
}
}
}
pub struct FindCaptures<'r, 't> {
re: &'r Regex,
search: &'t str,
last_match: Option<usize>,
last_end: usize,
}
impl<'r, 't> Iterator for FindCaptures<'r, 't> {
type Item = Captures<'t>;
fn next(&mut self) -> Option<Captures<'t>> {
if self.last_end > self.search.len() {
return None
}
let mut caps = self.re.alloc_captures();
if !exec(self.re, &mut caps, self.search, self.last_end) {
return None
}
let (s, e) = (caps[0].unwrap(), caps[1].unwrap());
if e == s && Some(self.last_end) == self.last_match {
if self.last_end >= self.search.len() {
return None;
}
self.last_end += self.search[self.last_end..].chars()
.next().unwrap().len_utf8();
return self.next()
}
self.last_end = e;
self.last_match = Some(self.last_end);
Some(Captures::new(self.re, self.search, caps))
}
}
pub struct FindMatches<'r, 't> {
re: &'r Regex,
search: &'t str,
last_match: Option<usize>,
last_end: usize,
}
impl<'r, 't> Iterator for FindMatches<'r, 't> {
type Item = (usize, usize);
fn next(&mut self) -> Option<(usize, usize)> {
if self.last_end > self.search.len() {
return None
}
let mut caps = [None, None];
if !exec(self.re, &mut caps, self.search, self.last_end) {
return None;
}
let (s, e) = (caps[0].unwrap(), caps[1].unwrap());
if e == s && Some(self.last_end) == self.last_match {
if self.last_end >= self.search.len() {
return None;
}
self.last_end += self.search[self.last_end..].chars()
.next().unwrap().len_utf8();
return self.next()
}
self.last_end = e;
self.last_match = Some(self.last_end);
Some((s, e))
}
}
#[cfg(feature = "pattern")]
pub struct RegexSearcher<'r, 't> {
it: FindMatches<'r, 't>,
last_step_end: usize,
next_match: Option<(usize, usize)>,
}
#[cfg(feature = "pattern")]
impl<'r, 't> Pattern<'t> for &'r Regex {
type Searcher = RegexSearcher<'r, 't>;
fn into_searcher(self, haystack: &'t str) -> RegexSearcher<'r, 't> {
RegexSearcher {
it: self.find_iter(haystack),
last_step_end: 0,
next_match: None,
}
}
}
#[cfg(feature = "pattern")]
unsafe impl<'r, 't> Searcher<'t> for RegexSearcher<'r, 't> {
#[inline]
fn haystack(&self) -> &'t str {
self.it.search
}
#[inline]
fn next(&mut self) -> SearchStep {
if let Some((s, e)) = self.next_match {
self.next_match = None;
self.last_step_end = e;
return SearchStep::Match(s, e);
}
match self.it.next() {
None => {
if self.last_step_end < self.haystack().len() {
let last = self.last_step_end;
self.last_step_end = self.haystack().len();
SearchStep::Reject(last, self.haystack().len())
} else {
SearchStep::Done
}
}
Some((s, e)) => {
if s == self.last_step_end {
self.last_step_end = e;
SearchStep::Match(s, e)
} else {
self.next_match = Some((s, e));
let last = self.last_step_end;
self.last_step_end = s;
SearchStep::Reject(last, s)
}
}
}
}
}
fn exec(re: &Regex, caps: &mut CaptureIdxs, text: &str, start: usize) -> bool {
match *re {
Regex::Native(ExNative { ref prog, .. }) => (*prog)(caps, text, start),
Regex::Dynamic(ref prog) => prog.exec(caps, text, start),
}
}
#[cfg(test)]
mod test {
use super::{NoExpand, Regex};
#[test]
fn test_simple_expand() {
let re = Regex::new(r"(\w) (\w)").unwrap();
assert_eq!(re.replace_all("a b", "$2 $1"), "b a");
}
#[test]
fn test_literal_dollar() {
let re = Regex::new(r"(\w+) (\w+)").unwrap();
assert_eq!(re.replace_all("a b", "$1"), "a");
assert_eq!(re.replace_all("a b", "$$1"), "$1"); assert_eq!(re.replace_all("a b", "$2 $$c $1"), "b $c a");
}
#[test]
fn test_no_expand() {
let re = Regex::new(r"(\w+)").unwrap();
assert_eq!(re.replace_all("a", NoExpand("$$1")), "$$1");
assert_eq!(re.replace_all("a", NoExpand("$1")), "$1");
}
#[test]
fn test_capture_names() {
let re = Regex::new(r"(.)(?P<a>.)").unwrap();
assert_eq!(re.capture_names().size_hint(), (3, Some(3)));
assert_eq!(re.capture_names().collect::<Vec<_>>(), [None, None, Some("a")]);
}
#[test]
fn test_cap_index() {
let re = Regex::new(r"^(?P<name>.+)$").unwrap();
let cap = re.captures("abc").unwrap();
assert_eq!(&cap[0], "abc");
assert_eq!(&cap[1], "abc");
assert_eq!(&cap["name"], "abc");
}
#[test]
#[should_panic]
fn test_cap_index_panic_usize() {
let re = Regex::new(r"^(?P<name>.+)$").unwrap();
let cap = re.captures("abc").unwrap();
let _ = cap[2];
}
#[test]
#[should_panic]
fn test_cap_index_panic_name() {
let re = Regex::new(r"^(?P<name>.+)$").unwrap();
let cap = re.captures("abc").unwrap();
let _ = cap["bad name"];
}
}