Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trailing zero between dot and exponential #7956

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.1
1.
1E+1
1E-1
1.E+1
1.0E+1
1.1E+1
13 changes: 8 additions & 5 deletions crates/ruff_python_formatter/src/expression/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn normalize_floating_number(input: &str) -> Cow<str> {

let mut chars = input.char_indices();

let fraction_ends_with_dot = if let Some((index, '.')) = chars.next() {
let mut prev_char_is_dot = if let Some((index, '.')) = chars.next() {
// Add a leading `0` if `input` starts with `.`.
output.push('0');
output.push('.');
Expand All @@ -155,8 +155,8 @@ fn normalize_floating_number(input: &str) -> Cow<str> {
loop {
match chars.next() {
Some((index, c @ ('e' | 'E'))) => {
if fraction_ends_with_dot {
// Add `0` if fraction part ends with `.`.
if prev_char_is_dot {
// Add `0` if the `e` immediately follows a `.` (e.g., `1.e1`).
output.push_str(&input[last_index..index]);
output.push('0');
last_index = index;
Expand All @@ -177,9 +177,12 @@ fn normalize_floating_number(input: &str) -> Cow<str> {

break;
}
Some(_) => continue,
Some((_index, c)) => {
prev_char_is_dot = c == '.';
continue;
}
None => {
if input.ends_with('.') {
if prev_char_is_dot {
// Add `0` if fraction part ends with `.`.
output.push_str(&input[last_index..]);
output.push('0');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/number.py
---
## Input
```py
.1
1.
1E+1
1E-1
1.E+1
1.0E+1
1.1E+1
```

## Output
```py
0.1
1.0
1e1
1e-1
1.0e1
1.0e1
1.1e1
```



Loading