Skip to content

Commit

Permalink
style: start using rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Aug 3, 2019
1 parent 341f207 commit 0e96af4
Show file tree
Hide file tree
Showing 99 changed files with 36,651 additions and 13,694 deletions.
32 changes: 18 additions & 14 deletions bench/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ fn main() {

if env::var("CARGO_FEATURE_RE_DPHOBOS_DMD").is_ok() {
process::Command::new("dmd")
.arg("--version")
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.spawn()
.unwrap();
.arg("--version")
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.spawn()
.unwrap();

let out_dir = env::var("OUT_DIR").unwrap();
let out_file = &format!("-of={}/libdphobos-dmd.a", out_dir);
let is_compile_time = env::var("CARGO_FEATURE_RE_DPHOBOS_DMD_CT").is_ok();
let extra_args = if is_compile_time { vec!["-version=CtRegex"] } else { vec![] };
let is_compile_time =
env::var("CARGO_FEATURE_RE_DPHOBOS_DMD_CT").is_ok();
let extra_args =
if is_compile_time { vec!["-version=CtRegex"] } else { vec![] };

let res = process::Command::new("dmd")
.arg("-w")
Expand Down Expand Up @@ -99,17 +101,19 @@ fn main() {

if env::var("CARGO_FEATURE_RE_DPHOBOS_LDC").is_ok() {
process::Command::new("ldc")
.arg("--version")
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.spawn()
.unwrap();
.arg("--version")
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.spawn()
.unwrap();

let out_dir = env::var("OUT_DIR").unwrap();
let out_file = &format!("-of={}/libdphobos-ldc.a", out_dir);

let is_compile_time = env::var("CARGO_FEATURE_RE_DPHOBOS_LDC_CT").is_ok();
let extra_args = if is_compile_time { vec!["-d-version=CtRegex"] } else { vec![] };
let is_compile_time =
env::var("CARGO_FEATURE_RE_DPHOBOS_LDC_CT").is_ok();
let extra_args =
if is_compile_time { vec!["-d-version=CtRegex"] } else { vec![] };

let res = process::Command::new("ldc")
.arg("-w")
Expand Down
29 changes: 14 additions & 15 deletions bench/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ cfg_if! {
// defined below. Effectively, it allows us to use the same tests for both
// native and dynamic regexes.
macro_rules! regex {
($re:expr) => { ::Regex::new(&$re.to_owned()).unwrap() }
($re:expr) => {
::Regex::new(&$re.to_owned()).unwrap()
};
}

cfg_if! {
Expand Down Expand Up @@ -119,7 +121,7 @@ cfg_if! {
macro_rules! bench_match {
($name:ident, $pattern:expr, $haystack:expr) => {
bench_is_match!($name, true, regex!($pattern), $haystack);
}
};
}

// USAGE: bench_not_match!(name, pattern, haystack)
Expand All @@ -136,7 +138,7 @@ macro_rules! bench_match {
macro_rules! bench_not_match {
($name:ident, $pattern:expr, $haystack:expr) => {
bench_is_match!($name, false, regex!($pattern), $haystack);
}
};
}

// USAGE: bench_is_match!(name, is_match, regex, haystack)
Expand Down Expand Up @@ -182,7 +184,7 @@ macro_rules! bench_is_match {
}
});
}
}
};
}

// USAGE: bench_find!(name, pattern, count, haystack)
Expand Down Expand Up @@ -214,7 +216,7 @@ macro_rules! bench_find {
assert_eq!($count, count)
});
}
}
};
}

// USAGE: bench_captures!(name, pattern, groups, haystack);
Expand All @@ -229,7 +231,6 @@ macro_rules! bench_find {
// the capture groups in question.
macro_rules! bench_captures {
($name:ident, $pattern:expr, $count:expr, $haystack:expr) => {

#[cfg(feature = "re-rust")]
#[bench]
fn $name(b: &mut Bencher) {
Expand All @@ -242,14 +243,12 @@ macro_rules! bench_captures {
let re = RE.lock().unwrap();
let text = TEXT.lock().unwrap();
b.bytes = text.len() as u64;
b.iter(|| {
match re.captures(&text) {
None => assert!(false, "no captures"),
Some(caps) => assert_eq!($count + 1, caps.len()),
}
b.iter(|| match re.captures(&text) {
None => assert!(false, "no captures"),
Some(caps) => assert_eq!($count + 1, caps.len()),
});
}
}
};
}

// USAGE: bench_is_match_set!(name, is_match, regex, haystack)
Expand All @@ -275,9 +274,9 @@ macro_rules! bench_is_match_set {
}
});
}
}
};
}

// USAGE: bench_matches_set!(name, is_match, regex, haystack)
macro_rules! bench_matches_set {
($name:ident, $is_match:expr, $re:expr, $haystack:expr) => {
Expand All @@ -301,7 +300,7 @@ macro_rules! bench_matches_set {
}
});
}
}
};
}

cfg_if! {
Expand Down
35 changes: 11 additions & 24 deletions bench/src/ffi/d_phobos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,17 @@ impl Regex {
}

pub fn is_match(&self, text: &str) -> bool {
unsafe {
d_phobos_regex_is_match(self.re, text.into())
}
unsafe { d_phobos_regex_is_match(self.re, text.into()) }
}

pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindMatches<'r, 't> {
FindMatches {
re: self,
text: text,
last_end: 0,
last_match: None,
}
FindMatches { re: self, text: text, last_end: 0, last_match: None }
}

fn find_at(&self, text: &str, start: usize) -> Option<(usize, usize)> {
let (mut s, mut e): (usize, usize) = (0, 0);
let matched = unsafe {
d_phobos_regex_find_at(
self.re,
text.into(),
start,
&mut s,
&mut e,
)
d_phobos_regex_find_at(self.re, text.into(), start, &mut s, &mut e)
};
if matched {
Some((s, e))
Expand Down Expand Up @@ -99,17 +86,17 @@ impl<'a> From<&'a str> for d_string {
}
}

extern {
extern "C" {
fn rt_init() -> i32;
fn rt_term() -> i32;
fn d_phobos_regex_new(s: d_string) -> *mut d_regex;
fn d_phobos_regex_free(r: *mut d_regex);
fn d_phobos_regex_is_match(r: *mut d_regex, s: d_string) -> bool;
fn d_phobos_regex_find_at(r: *mut d_regex,
s: d_string,
start: usize,
match_start: *mut usize,
match_end: *mut usize)
-> bool;
fn d_phobos_regex_find_at(
r: *mut d_regex,
s: d_string,
start: usize,
match_start: *mut usize,
match_end: *mut usize,
) -> bool;
}

7 changes: 2 additions & 5 deletions bench/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ pub mod onig;
pub mod pcre1;
#[cfg(feature = "re-pcre2")]
pub mod pcre2;
#[cfg(any(
feature = "re-stdcpp",
feature = "re-boost",
))]
pub mod stdcpp;
#[cfg(feature = "re-re2")]
pub mod re2;
#[cfg(any(feature = "re-stdcpp", feature = "re-boost",))]
pub mod stdcpp;
#[cfg(feature = "re-tcl")]
pub mod tcl;
16 changes: 9 additions & 7 deletions bench/src/ffi/onig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ impl Regex {

pub fn is_match(&self, text: &str) -> bool {
// Gah. onig's is_match function is anchored, but find is not.
self.0.search_with_options(
text,
0,
text.len(),
onig::SearchOptions::SEARCH_OPTION_NONE,
None,
).is_some()
self.0
.search_with_options(
text,
0,
text.len(),
onig::SearchOptions::SEARCH_OPTION_NONE,
None,
)
.is_some()
}

pub fn find_iter<'r, 't>(
Expand Down
68 changes: 31 additions & 37 deletions bench/src/ffi/pcre1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

#![allow(non_snake_case)]

use std::ffi::{CString, CStr};
use std::ffi::{CStr, CString};
use std::fmt;
use std::ptr;

use libc::{c_char, c_int, c_void};
use libpcre_sys::{
PCRE_UTF8, PCRE_NO_UTF8_CHECK, PCRE_ERROR_NOMATCH,
pcre, pcre_extra,
pcre_compile, pcre_free, pcre_study, pcre_free_study, pcre_exec,
pcre, pcre_compile, pcre_exec, pcre_extra, pcre_free, pcre_free_study,
pcre_study, PCRE_ERROR_NOMATCH, PCRE_NO_UTF8_CHECK, PCRE_UTF8,
};

const PCRE_UCP: c_int = 0x20000000;
Expand Down Expand Up @@ -53,32 +52,29 @@ impl Regex {
let pattern = CString::new(pattern.to_owned()).unwrap();
let mut errptr: *const c_char = ptr::null();
let mut erroffset: c_int = 0;
let code = unsafe { pcre_compile(
pattern.as_ptr(),
PCRE_UCP | PCRE_UTF8,
&mut errptr,
&mut erroffset,
ptr::null(),
) };
let code = unsafe {
pcre_compile(
pattern.as_ptr(),
PCRE_UCP | PCRE_UTF8,
&mut errptr,
&mut erroffset,
ptr::null(),
)
};
if code.is_null() {
let msg = unsafe {
CStr::from_ptr(errptr).to_str().unwrap().to_owned()
};
let msg =
unsafe { CStr::from_ptr(errptr).to_str().unwrap().to_owned() };
return Err(Error { msg: msg, offset: erroffset });
}

let extra = unsafe { pcre_study(
code,
PCRE_STUDY_JIT_COMPLETE,
&mut errptr,
) };
let extra =
unsafe { pcre_study(code, PCRE_STUDY_JIT_COMPLETE, &mut errptr) };
if extra.is_null() {
if errptr.is_null() {
panic!("unexpected error. Maybe JIT support isn't enabled?");
}
let msg = unsafe {
CStr::from_ptr(errptr).to_str().unwrap().to_owned()
};
let msg =
unsafe { CStr::from_ptr(errptr).to_str().unwrap().to_owned() };
return Err(Error { msg: msg, offset: 0 });
}
Ok(Regex { code: code, extra: extra })
Expand All @@ -89,26 +85,24 @@ impl Regex {
}

pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindMatches<'r, 't> {
FindMatches {
re: self,
text: text,
last_match_end: 0,
}
FindMatches { re: self, text: text, last_match_end: 0 }
}

fn find_at(&self, text: &str, start: usize) -> Option<(usize, usize)> {
const OVEC_SIZE: usize = 15 * 3; // hopefully enough for benchmarks?
let mut ovec: [c_int; OVEC_SIZE] = [0; OVEC_SIZE];
let err = unsafe { pcre_exec(
self.code,
self.extra,
text.as_ptr() as *const i8,
text.len() as c_int,
start as c_int,
PCRE_NO_UTF8_CHECK,
ovec.as_mut_ptr(),
OVEC_SIZE as c_int,
) };
let err = unsafe {
pcre_exec(
self.code,
self.extra,
text.as_ptr() as *const i8,
text.len() as c_int,
start as c_int,
PCRE_NO_UTF8_CHECK,
ovec.as_mut_ptr(),
OVEC_SIZE as c_int,
)
};
if err == PCRE_ERROR_NOMATCH {
None
} else if err < 0 {
Expand Down
Loading

0 comments on commit 0e96af4

Please sign in to comment.