-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from oli-obk/librarification
Lots of cleanups
- Loading branch information
Showing
11 changed files
with
1,042 additions
and
1,111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Datastructures and operations used for normalizing test output. | ||
use bstr::ByteSlice; | ||
use lazy_static::lazy_static; | ||
use regex::bytes::{Captures, Regex}; | ||
use std::borrow::Cow; | ||
use std::path::Path; | ||
|
||
/// A filter's match rule. | ||
#[derive(Clone, Debug)] | ||
pub enum Match { | ||
/// If the regex matches, the filter applies | ||
Regex(Regex), | ||
/// If the exact byte sequence is found, the filter applies | ||
Exact(Vec<u8>), | ||
/// Uses a heuristic to find backslashes in windows style paths | ||
PathBackslash, | ||
} | ||
|
||
impl Match { | ||
pub(crate) fn replace_all<'a>(&self, text: &'a [u8], replacement: &[u8]) -> Cow<'a, [u8]> { | ||
match self { | ||
Match::Regex(regex) => regex.replace_all(text, replacement), | ||
Match::Exact(needle) => text.replace(needle, replacement).into(), | ||
Match::PathBackslash => { | ||
lazy_static! { | ||
static ref PATH_RE: Regex = Regex::new( | ||
r"(?x) | ||
(?: | ||
# Match paths to files with extensions that don't include spaces | ||
\\(?:[\pL\pN.\-_']+[/\\])*[\pL\pN.\-_']+\.\pL+ | ||
| | ||
# Allow spaces in absolute paths | ||
[A-Z]:\\(?:[\pL\pN.\-_'\ ]+[/\\])+ | ||
)", | ||
) | ||
.unwrap(); | ||
} | ||
|
||
PATH_RE.replace_all(text, |caps: &Captures<'_>| { | ||
caps[0].replace(r"\", replacement) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<&'_ Path> for Match { | ||
fn from(v: &Path) -> Self { | ||
let mut v = v.display().to_string(); | ||
// Normalize away windows canonicalized paths. | ||
if v.starts_with(r"\\?\") { | ||
v.drain(0..4); | ||
} | ||
let mut v = v.into_bytes(); | ||
// Normalize paths on windows to use slashes instead of backslashes, | ||
// So that paths are rendered the same on all systems. | ||
for c in &mut v { | ||
if *c == b'\\' { | ||
*c = b'/'; | ||
} | ||
} | ||
Self::Exact(v) | ||
} | ||
} | ||
|
||
impl From<Regex> for Match { | ||
fn from(v: Regex) -> Self { | ||
Self::Regex(v) | ||
} | ||
} |
Oops, something went wrong.