Skip to content

Commit 3dff918

Browse files
committed
Remove dependency on regex
1 parent c1aa017 commit 3dff918

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

src/Cargo.lock

-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_lint/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ version = "0.0.0"
66
[lib]
77
name = "rustc_lint"
88
path = "lib.rs"
9-
crate-types = ["rlib", "dylib"]
9+
crate-type = ["dylib"]
1010
test = false
1111

1212
[dependencies]
@@ -15,5 +15,3 @@ rustc = { path = "../librustc" }
1515
rustc_const_eval = { path = "../librustc_const_eval" }
1616
syntax = { path = "../libsyntax" }
1717
syntax_pos = { path = "../libsyntax_pos" }
18-
lazy_static = "1.0"
19-
regex = "0.2.3"

src/librustc_lint/bad_style.rs

+32-23
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ use syntax_pos::Span;
2121
use rustc::hir::{self, PatKind};
2222
use rustc::hir::intravisit::FnKind;
2323

24-
use regex::Regex;
25-
26-
lazy_static! {
27-
static ref ALPHABETIC_UNDERSCORE: Regex =
28-
Regex::new("([[:alpha:]])_+|_+([[:alpha:]])").unwrap();
29-
}
30-
3124
#[derive(PartialEq)]
3225
pub enum MethodLateContext {
3326
TraitAutoImpl,
@@ -60,6 +53,10 @@ pub struct NonCamelCaseTypes;
6053

6154
impl NonCamelCaseTypes {
6255
fn check_case(&self, cx: &LateContext, sort: &str, name: ast::Name, span: Span) {
56+
fn char_has_case(c: char) -> bool {
57+
c.is_lowercase() || c.is_uppercase()
58+
}
59+
6360
fn is_camel_case(name: ast::Name) -> bool {
6461
let name = name.as_str();
6562
if name.is_empty() {
@@ -70,25 +67,37 @@ impl NonCamelCaseTypes {
7067
// start with a non-lowercase letter rather than non-uppercase
7168
// ones (some scripts don't have a concept of upper/lowercase)
7269
!name.is_empty() && !name.chars().next().unwrap().is_lowercase() &&
73-
!name.contains("__") && !ALPHABETIC_UNDERSCORE.is_match(name)
70+
!name.contains("__") && !name.chars().collect::<Vec<_>>().windows(2).any(|pair| {
71+
// contains a capitalisable character followed by, or preceded by, an underscore
72+
char_has_case(pair[0]) && pair[1] == '_' ||
73+
char_has_case(pair[1]) && pair[0] == '_'
74+
})
7475
}
7576

7677
fn to_camel_case(s: &str) -> String {
77-
let s = s.trim_matches('_')
78-
.split('_')
79-
.map(|word| {
80-
word.chars().enumerate().map(|(i, c)| if i == 0 {
81-
c.to_uppercase().collect::<String>()
82-
} else {
83-
c.to_lowercase().collect()
84-
})
85-
.collect::<Vec<_>>()
86-
.concat()
87-
})
88-
.filter(|x| !x.is_empty())
89-
.collect::<Vec<_>>()
90-
.join("_");
91-
ALPHABETIC_UNDERSCORE.replace_all(s.as_str(), "$1$2").to_string()
78+
s.trim_matches('_')
79+
.split('_')
80+
.map(|word| {
81+
word.chars().enumerate().map(|(i, c)| if i == 0 {
82+
c.to_uppercase().collect::<String>()
83+
} else {
84+
c.to_lowercase().collect()
85+
})
86+
.collect::<Vec<_>>()
87+
.concat()
88+
})
89+
.filter(|x| !x.is_empty())
90+
.collect::<Vec<_>>()
91+
.iter().fold((String::new(), None), |(acc, prev): (String, Option<&String>), next| {
92+
// separate two components with an underscore if their boundary cannot
93+
// be distinguished using a uppercase/lowercase case distinction
94+
let join = if let Some(prev) = prev {
95+
let l = prev.chars().last().unwrap();
96+
let f = next.chars().next().unwrap();
97+
!char_has_case(l) && !char_has_case(f)
98+
} else { false };
99+
(acc + if join { "_" } else { "" } + next, Some(next))
100+
}).0
92101
}
93102

94103
if !is_camel_case(name) {

src/librustc_lint/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ extern crate rustc;
4141
extern crate log;
4242
extern crate rustc_const_eval;
4343
extern crate syntax_pos;
44-
#[macro_use]
45-
extern crate lazy_static;
46-
extern crate regex;
4744

4845
use rustc::lint;
4946
use rustc::middle;

0 commit comments

Comments
 (0)