Skip to content

Commit

Permalink
Box other strings
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 8, 2024
1 parent 5ff99e6 commit 2e75d58
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ impl Violation for HardcodedBindAllInterfaces {
pub(crate) fn hardcoded_bind_all_interfaces(checker: &mut Checker, string: StringLike) {
let is_bind_all_interface = match string {
StringLike::StringLiteral(ast::ExprStringLiteral { value, .. }) => value == "0.0.0.0",
StringLike::FStringLiteral(ast::FStringLiteralElement { value, .. }) => value == "0.0.0.0",
StringLike::FStringLiteral(ast::FStringLiteralElement { value, .. }) => {
&**value == "0.0.0.0"
}
StringLike::BytesLiteral(_) => return,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/flynt/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn to_f_string_expression_element(inner: &Expr) -> ast::FStringElement {
/// Convert a string to a [`ast::FStringElement::Literal`].
pub(super) fn to_f_string_literal_element(s: &str) -> ast::FStringElement {
ast::FStringElement::Literal(ast::FStringLiteralElement {
value: s.to_owned(),
value: s.to_string().into_boxed_str(),
range: TextRange::default(),
})
}
Expand Down Expand Up @@ -53,7 +53,7 @@ pub(super) fn to_f_string_element(expr: &Expr) -> Option<ast::FStringElement> {
match expr {
Expr::StringLiteral(ast::ExprStringLiteral { value, range }) => {
Some(ast::FStringElement::Literal(ast::FStringLiteralElement {
value: value.to_string(),
value: value.to_string().into_boxed_str(),
range: *range,
}))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ pub struct ComparableBytesLiteral<'a> {
impl<'a> From<&'a ast::BytesLiteral> for ComparableBytesLiteral<'a> {
fn from(bytes_literal: &'a ast::BytesLiteral) -> Self {
Self {
value: bytes_literal.value.as_slice(),
value: &bytes_literal.value,
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ impl Ranged for FStringExpressionElement {
#[derive(Clone, Debug, PartialEq)]
pub struct FStringLiteralElement {
pub range: TextRange,
pub value: String,
pub value: Box<str>,
}

impl Ranged for FStringLiteralElement {
Expand All @@ -962,7 +962,7 @@ impl Deref for FStringLiteralElement {
type Target = str;

fn deref(&self) -> &Self::Target {
self.value.as_str()
&self.value
}
}

Expand Down Expand Up @@ -1607,7 +1607,7 @@ impl Default for BytesLiteralValueInner {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BytesLiteral {
pub range: TextRange,
pub value: Vec<u8>,
pub value: Box<[u8]>,
}

impl Ranged for BytesLiteral {
Expand All @@ -1620,7 +1620,7 @@ impl Deref for BytesLiteral {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.value.as_slice()
&self.value
}
}

Expand Down
14 changes: 7 additions & 7 deletions crates/ruff_python_parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ impl StringParser {

let mut value = String::with_capacity(self.source.len());
loop {
// Add the characters before the escape sequence to the string.
let before_with_slash = self.skip_bytes(index + 1);
let before = &before_with_slash[..before_with_slash.len() - 1];
// Add the characters before the escape sequence (or curly brace) to the string.
let before_with_slash_or_brace = self.skip_bytes(index + 1);
let before = &before_with_slash_or_brace[..before_with_slash_or_brace.len() - 1];
value.push_str(before);

// Add the escaped character to the string.
Expand Down Expand Up @@ -284,7 +284,7 @@ impl StringParser {
}

Ok(ast::FStringElement::Literal(ast::FStringLiteralElement {
value,
value: value.into_boxed_str(),
range: self.range,
}))
}
Expand All @@ -305,15 +305,15 @@ impl StringParser {
if self.kind.is_raw() {
// For raw strings, no escaping is necessary.
return Ok(StringType::Bytes(ast::BytesLiteral {
value: self.source.into_bytes(),
value: self.source.into_boxed_bytes(),
range: self.range,
}));
}

let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else {
// If the string doesn't contain any escape sequences, return the owned string.
return Ok(StringType::Bytes(ast::BytesLiteral {
value: self.source.into_bytes(),
value: self.source.into_boxed_bytes(),
range: self.range,
}));
};
Expand Down Expand Up @@ -349,7 +349,7 @@ impl StringParser {
}

Ok(StringType::Bytes(ast::BytesLiteral {
value,
value: value.into_boxed_slice(),
range: self.range,
}))
}
Expand Down

0 comments on commit 2e75d58

Please sign in to comment.