Merge #7567
7567: Remove unnecessary allocs in case_conv r=Veykril a=Veykril and some replace unwraps bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
c72b0c3719
@ -5,7 +5,7 @@
|
|||||||
// from file /compiler/rustc_lint/src/nonstandard_style.rs
|
// from file /compiler/rustc_lint/src/nonstandard_style.rs
|
||||||
|
|
||||||
/// Converts an identifier to an UpperCamelCase form.
|
/// Converts an identifier to an UpperCamelCase form.
|
||||||
/// Returns `None` if the string is already is UpperCamelCase.
|
/// Returns `None` if the string is already in UpperCamelCase.
|
||||||
pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
|
pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
|
||||||
if is_camel_case(ident) {
|
if is_camel_case(ident) {
|
||||||
return None;
|
return None;
|
||||||
@ -17,7 +17,7 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
|
|||||||
.split('_')
|
.split('_')
|
||||||
.filter(|component| !component.is_empty())
|
.filter(|component| !component.is_empty())
|
||||||
.map(|component| {
|
.map(|component| {
|
||||||
let mut camel_cased_component = String::new();
|
let mut camel_cased_component = String::with_capacity(component.len());
|
||||||
|
|
||||||
let mut new_word = true;
|
let mut new_word = true;
|
||||||
let mut prev_is_lower_case = true;
|
let mut prev_is_lower_case = true;
|
||||||
@ -30,9 +30,9 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if new_word {
|
if new_word {
|
||||||
camel_cased_component.push_str(&c.to_uppercase().to_string());
|
camel_cased_component.extend(c.to_uppercase());
|
||||||
} else {
|
} else {
|
||||||
camel_cased_component.push_str(&c.to_lowercase().to_string());
|
camel_cased_component.extend(c.to_lowercase());
|
||||||
}
|
}
|
||||||
|
|
||||||
prev_is_lower_case = c.is_lowercase();
|
prev_is_lower_case = c.is_lowercase();
|
||||||
@ -41,16 +41,16 @@ pub(crate) fn to_camel_case(ident: &str) -> Option<String> {
|
|||||||
|
|
||||||
camel_cased_component
|
camel_cased_component
|
||||||
})
|
})
|
||||||
.fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
|
.fold((String::new(), None), |(acc, prev): (_, Option<String>), next| {
|
||||||
// separate two components with an underscore if their boundary cannot
|
// separate two components with an underscore if their boundary cannot
|
||||||
// be distinguished using a uppercase/lowercase case distinction
|
// be distinguished using a uppercase/lowercase case distinction
|
||||||
let join = if let Some(prev) = prev {
|
let join = prev
|
||||||
let l = prev.chars().last().unwrap();
|
.and_then(|prev| {
|
||||||
let f = next.chars().next().unwrap();
|
let f = next.chars().next()?;
|
||||||
!char_has_case(l) && !char_has_case(f)
|
let l = prev.chars().last()?;
|
||||||
} else {
|
Some(!char_has_case(l) && !char_has_case(f))
|
||||||
false
|
})
|
||||||
};
|
.unwrap_or(false);
|
||||||
(acc + if join { "_" } else { "" } + &next, Some(next))
|
(acc + if join { "_" } else { "" } + &next, Some(next))
|
||||||
})
|
})
|
||||||
.0;
|
.0;
|
||||||
@ -92,14 +92,12 @@ fn is_camel_case(name: &str) -> bool {
|
|||||||
let mut fst = None;
|
let mut fst = None;
|
||||||
// start with a non-lowercase letter rather than non-uppercase
|
// start with a non-lowercase letter rather than non-uppercase
|
||||||
// ones (some scripts don't have a concept of upper/lowercase)
|
// ones (some scripts don't have a concept of upper/lowercase)
|
||||||
!name.chars().next().unwrap().is_lowercase()
|
name.chars().next().map_or(true, |c| !c.is_lowercase())
|
||||||
&& !name.contains("__")
|
&& !name.contains("__")
|
||||||
&& !name.chars().any(|snd| {
|
&& !name.chars().any(|snd| {
|
||||||
let ret = match (fst, snd) {
|
let ret = match fst {
|
||||||
(None, _) => false,
|
None => false,
|
||||||
(Some(fst), snd) => {
|
Some(fst) => char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_',
|
||||||
char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
fst = Some(snd);
|
fst = Some(snd);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user