mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-17 13:58:37 +00:00
Move python
into its own ruff_python
crate (#2593)
This commit is contained in:
parent
ecc9f5de99
commit
87d0aa5561
33 changed files with 158 additions and 119 deletions
24
crates/ruff_python/src/identifiers.rs
Normal file
24
crates/ruff_python/src/identifiers.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
/// Returns `true` if a string is a valid Python identifier (e.g., variable
|
||||
/// name).
|
||||
pub fn is_identifier(s: &str) -> bool {
|
||||
// Is the first character a letter or underscore?
|
||||
if !s
|
||||
.chars()
|
||||
.next()
|
||||
.map_or(false, |c| c.is_alphabetic() || c == '_')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Are the rest of the characters letters, digits, or underscores?
|
||||
s.chars().skip(1).all(|c| c.is_alphanumeric() || c == '_')
|
||||
}
|
||||
|
||||
/// Returns `true` if a string is a private identifier, such that, when the
|
||||
/// identifier is defined in a class definition, it will be mangled prior to
|
||||
/// code generation.
|
||||
///
|
||||
/// See: <https://docs.python.org/3.5/reference/expressions.html?highlight=mangling#index-5>.
|
||||
pub fn is_mangled_private(id: &str) -> bool {
|
||||
id.starts_with("__") && !id.ends_with("__")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue