Skip to content

Commit 08627c8

Browse files
committed
fix: use utf-8 length instead of byte length
1 parent 8213b1f commit 08627c8

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ Types of changes
1717
- Security in case of vulnerabilities.
1818
-->
1919

20+
### Fixed
21+
22+
- Multiline strings are handled as utf-8 correctly, preventing panics
23+
on utf-8 whitespace like:
24+
25+
```nix
26+
''
27+
foo
28+
\u{2002}bar
29+
''
30+
```
31+
2032
## [0.5.0] - 2022-02-23
2133

2234
### Changed

src/alejandra_engine/src/rules/string.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn rule(
7373
if !line.is_empty() {
7474
indentation = usize::min(
7575
indentation,
76-
line.len() - line.trim_start().len(),
76+
line.chars().count() - line.trim_start().chars().count(),
7777
);
7878
}
7979
}
@@ -85,8 +85,8 @@ pub fn rule(
8585
lines = lines
8686
.iter()
8787
.map(|line| {
88-
if indentation < line.len() {
89-
line[indentation..line.len()].to_string()
88+
if indentation < line.chars().count() {
89+
line.chars().skip(indentation).collect::<String>()
9090
} else {
9191
line.to_string()
9292
}

src/alejandra_engine/tests/cases/string/in

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
''
3+
foo
4+
 bar
5+
''
26
""
37
###
48
"

src/alejandra_engine/tests/cases/string/out

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
''
3+
foo
4+
bar
5+
''
26
""
37
###
48
"

0 commit comments

Comments
 (0)