Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fancy_regex instead of onig #4

Merged
merged 4 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/core"]
path = src/core
url = git@github.com:ua-parser/uap-core.git
url = https://github.com/ua-parser/uap-core.git
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ serde = "1.0.99"
serde_yaml = "0.8.9"
serde_derive = "1.0.99"
derive_more = "0.15.0"
onig = "5.0.0"
fancy-regex = "0.3.1"
2 changes: 1 addition & 1 deletion src/core
37 changes: 23 additions & 14 deletions src/parser/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::*;

#[derive(Debug, Display, From)]
pub enum Error {
Onig(onig::Error),
Regex(fancy_regex::Error),
}

#[derive(Debug)]
pub struct Matcher {
regex: onig::Regex,
regex: fancy_regex::Regex,
device_replacement: Option<String>,
brand_replacement: Option<String>,
model_replacement: Option<String>,
Expand All @@ -17,26 +17,34 @@ impl SubParser for Matcher {
type Item = Device;

fn try_parse(&self, text: &str) -> Option<Self::Item> {
if let Some(captures) = self.regex.captures(text) {
if let Ok(Some(captures)) = self.regex.captures(text) {
let family: String =
if let Some(device_replacement) = &self.device_replacement {
replace(&device_replacement, &captures)
} else {
captures.at(1).map(str::to_string)?
captures
.get(1)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)?
};

let brand: Option<String> =
if let Some(brand_replacement) = &self.brand_replacement {
none_if_empty(replace(&brand_replacement, &captures))
} else {
captures.at(2).map(str::to_string)
None
};

let model: Option<String> =
if let Some(model_replacement) = &self.model_replacement {
none_if_empty(replace(&model_replacement, &captures))
} else {
captures.at(3).map(str::to_string)
captures
.get(1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior was ported from the official Python implementation and is necessary for making the tests pass. I have read the "specification" document of uap-core and I have no idea what the intended behavior is or why this used to work before.

.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)
};

Some(Device {
Expand All @@ -52,14 +60,15 @@ impl SubParser for Matcher {

impl Matcher {
pub fn try_from(entry: DeviceParserEntry) -> Result<Matcher, Error> {
let options = if Some("i") == entry.regex_flag.as_ref().map(String::as_str) {
onig::RegexOptions::REGEX_OPTION_IGNORECASE
} else {
onig::RegexOptions::REGEX_OPTION_NONE
};

let regex =
onig::Regex::with_options(&entry.regex, options, onig::Syntax::default());
let regex_with_flags =
if !entry.regex_flag.as_ref().map_or(true, String::is_empty) {
format!("(?{}){}", entry.regex_flag.unwrap_or_default(), entry.regex)
} else {
entry.regex.to_owned()
};
let regex = fancy_regex::RegexBuilder::new(&regex_with_flags)
.delegate_size_limit(20 * (1 << 20))
.build();

Ok(Matcher {
regex: regex?,
Expand Down
6 changes: 3 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ pub(self) fn none_if_empty<T: AsRef<str>>(s: T) -> Option<T> {
}
}

pub(self) fn replace(replacement: &str, captures: &onig::Captures) -> String {
if replacement.contains('$') && !captures.is_empty() {
pub(self) fn replace(replacement: &str, captures: &fancy_regex::Captures) -> String {
if replacement.contains('$') && captures.len() > 0 {
(1..=captures.len())
.fold(replacement.to_owned(), |state: String, i: usize| {
let group = captures.at(i).unwrap_or_default();
let group = captures.get(i).map(|x| x.as_str()).unwrap_or("");
state.replace(&format!("${}", i), &group)
})
.trim()
Expand Down
38 changes: 29 additions & 9 deletions src/parser/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::*;

#[derive(Debug, Display, From)]
pub enum Error {
Onig(onig::Error),
Regex(fancy_regex::Error),
}

#[derive(Debug)]
pub struct Matcher {
regex: onig::Regex,
regex: fancy_regex::Regex,
os_replacement: Option<String>,
os_v1_replacement: Option<String>,
os_v2_replacement: Option<String>,
Expand All @@ -18,35 +18,55 @@ impl SubParser for Matcher {
type Item = OS;

fn try_parse(&self, text: &str) -> Option<Self::Item> {
if let Some(captures) = self.regex.captures(text) {
if let Ok(Some(captures)) = self.regex.captures(text) {
let family: String = if let Some(os_replacement) = &self.os_replacement {
replace(&os_replacement, &captures)
} else {
captures.at(1).map(str::to_string)?
captures
.get(1)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)?
};

let major: Option<String> =
if let Some(os_v1_replacement) = &self.os_v1_replacement {
none_if_empty(replace(&os_v1_replacement, &captures))
} else {
captures.at(2).map(str::to_string)
captures
.get(2)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)
};

let minor: Option<String> =
if let Some(os_v2_replacement) = &self.os_v2_replacement {
none_if_empty(replace(&os_v2_replacement, &captures))
} else {
captures.at(3).map(str::to_string)
captures
.get(3)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)
};

let patch: Option<String> =
if let Some(os_v3_replacement) = &self.os_v3_replacement {
none_if_empty(replace(&os_v3_replacement, &captures))
} else {
captures.at(4).map(str::to_string)
captures
.get(4)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)
};

let patch_minor: Option<String> = captures.at(5).map(str::to_string);
let patch_minor: Option<String> = captures
.get(5)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string);

Some(OS {
family,
Expand All @@ -63,7 +83,7 @@ impl SubParser for Matcher {

impl Matcher {
pub fn try_from(entry: OSParserEntry) -> Result<Matcher, Error> {
let regex = onig::Regex::new(&entry.regex);
let regex = fancy_regex::Regex::new(&entry.regex);

Ok(Matcher {
regex: regex?,
Expand Down
31 changes: 20 additions & 11 deletions src/parser/user_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use super::*;

#[derive(Debug, Display, From)]
pub enum Error {
Onig(onig::Error),
Regex(fancy_regex::Error),
}

#[derive(Debug)]
pub struct Matcher {
regex: onig::Regex,
regex: fancy_regex::Regex,
family_replacement: Option<String>,
v1_replacement: Option<String>,
v2_replacement: Option<String>,
Expand All @@ -18,34 +18,41 @@ impl SubParser for Matcher {
type Item = UserAgent;

fn try_parse(&self, text: &str) -> Option<Self::Item> {
if let Some(captures) = self.regex.captures(text) {
if let Ok(Some(captures)) = self.regex.captures(text) {
let family: String =
if let Some(family_replacement) = &self.family_replacement {
replace(&family_replacement, &captures)
} else {
captures.at(1).map(str::to_string)?
captures
.get(1)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(ToString::to_string)?
}
.to_owned();

let major = self.v1_replacement.to_owned().or_else(|| {
captures
.at(2)
.get(2)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(str::to_string)
.map(ToString::to_string)
});

let minor = self.v2_replacement.to_owned().or_else(|| {
captures
.at(3)
.get(3)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(str::to_string)
.map(ToString::to_string)
});

let patch = self.v3_replacement.to_owned().or_else(|| {
captures
.at(4)
.get(4)
.map(|x| x.as_str())
.and_then(none_if_empty)
.map(str::to_string)
.map(ToString::to_string)
});

Some(UserAgent {
Expand All @@ -62,7 +69,9 @@ impl SubParser for Matcher {

impl Matcher {
pub fn try_from(entry: UserAgentParserEntry) -> Result<Matcher, Error> {
let regex = onig::Regex::new(&entry.regex);
let regex = fancy_regex::RegexBuilder::new(&entry.regex)
.delegate_size_limit(20 * (1 << 20))
.build();

Ok(Matcher {
regex: regex?,
Expand Down