Skip to content

Commit f97a80e

Browse files
authored
Merge pull request RustPython#4372 from harupy/refactor-parse_formatted_value
Refactor `FStringParser.parse_formatted_value`
2 parents 1d8269f + f3a9d34 commit f97a80e

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

compiler/parser/src/fstring.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl FStringParser {
2626
mut chars: iter::Peekable<str::Chars<'a>>,
2727
nested: u8,
2828
) -> Result<(Vec<Expr>, iter::Peekable<str::Chars<'a>>), FStringErrorType> {
29-
let mut expression = String::from("{");
29+
let mut expression = String::new();
3030
let mut spec = None;
3131
let mut delims = Vec::new();
3232
let mut conversion = ConversionFlag::None;
@@ -67,14 +67,14 @@ impl FStringParser {
6767
Some('a') => ConversionFlag::Ascii,
6868
Some('r') => ConversionFlag::Repr,
6969
Some(_) => {
70-
return Err(if expression[1..].trim().is_empty() {
70+
return Err(if expression.trim().is_empty() {
7171
EmptyExpression
7272
} else {
7373
InvalidConversionFlag
7474
});
7575
}
7676
None => {
77-
return Err(if expression[1..].trim().is_empty() {
77+
return Err(if expression.trim().is_empty() {
7878
EmptyExpression
7979
} else {
8080
UnclosedLbrace
@@ -84,14 +84,14 @@ impl FStringParser {
8484

8585
if let Some(&peek) = chars.peek() {
8686
if peek != '}' && peek != ':' {
87-
return Err(if expression[1..].trim().is_empty() {
87+
return Err(if expression.trim().is_empty() {
8888
EmptyExpression
8989
} else {
9090
UnclosedLbrace
9191
});
9292
}
9393
} else {
94-
return Err(if expression[1..].trim().is_empty() {
94+
return Err(if expression.trim().is_empty() {
9595
EmptyExpression
9696
} else {
9797
UnclosedLbrace
@@ -156,15 +156,14 @@ impl FStringParser {
156156
}
157157
}
158158
'}' => {
159-
if expression[1..].trim().is_empty() {
159+
if expression.trim().is_empty() {
160160
return Err(EmptyExpression);
161161
}
162-
expression.push(ch);
163162

164163
let ret = if !self_documenting {
165164
vec![self.expr(ExprKind::FormattedValue {
166165
value: Box::new(
167-
parse_fstring_expr(&expression[1..expression.len() - 1])
166+
parse_fstring_expr(&expression)
168167
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
169168
),
170169
conversion: conversion as _,
@@ -173,9 +172,7 @@ impl FStringParser {
173172
} else {
174173
vec![
175174
self.expr(ExprKind::Constant {
176-
value: Constant::Str(
177-
expression[1..expression.len() - 1].to_owned() + "=",
178-
),
175+
value: Constant::Str(expression.to_owned() + "="),
179176
kind: None,
180177
}),
181178
self.expr(ExprKind::Constant {
@@ -184,7 +181,7 @@ impl FStringParser {
184181
}),
185182
self.expr(ExprKind::FormattedValue {
186183
value: Box::new(
187-
parse_fstring_expr(&expression[1..expression.len() - 1])
184+
parse_fstring_expr(&expression)
188185
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
189186
),
190187
conversion: (if conversion == ConversionFlag::None && spec.is_none()
@@ -226,7 +223,7 @@ impl FStringParser {
226223
}
227224
}
228225
}
229-
Err(if expression[1..].trim().is_empty() {
226+
Err(if expression.trim().is_empty() {
230227
EmptyExpression
231228
} else {
232229
UnclosedLbrace

0 commit comments

Comments
 (0)