From 47855cc2b179373d7d1f2cb37725571a9a2e9e98 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Fri, 27 Oct 2023 19:16:17 +0200 Subject: [PATCH] use `to_lowercase` in `str downcase` (#10850) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description as we can see in the [documentation of `str.to_lowercase`](https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase), not only ASCII symbols have lower and upper variants. - `str upcase` uses the correct method to convert the string https://github.com/nushell/nushell/blob/7ac5a01e2f5020a1295697edab727cd62a5844b7/crates/nu-command/src/strings/str_/case/upcase.rs#L93 - `str downcase` incorrectly converts only ASCII characters https://github.com/nushell/nushell/blob/7ac5a01e2f5020a1295697edab727cd62a5844b7/crates/nu-command/src/strings/str_/case/downcase.rs#L124 this PR uses `str.to_lower_case` instead of `str.to_ascii_lowercase` in `str downcase`. # User-Facing Changes - upcase still works fine ```nushell ~ l> "ὀδυσσεύς" | str upcase ὈΔΥΣΣΕΎΣ ``` - downcase now works :point_right: before ```nushell ~ l> "ὈΔΥΣΣΕΎΣ" | str downcase ὈΔΥΣΣΕΎΣ ``` :point_right: after ```nushell ~ l> "ὈΔΥΣΣΕΎΣ" | str downcase ὀδυσσεύς ``` # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :black_circle: `toolkit test` - :black_circle: `toolkit test stdlib` adds two tests - `non_ascii_upcase` - `non_ascii_downcase` # After Submitting --- .../nu-command/src/strings/str_/case/downcase.rs | 2 +- crates/nu-command/tests/commands/str_/mod.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/strings/str_/case/downcase.rs b/crates/nu-command/src/strings/str_/case/downcase.rs index 3918689deac97..a5984ffc9a3b7 100644 --- a/crates/nu-command/src/strings/str_/case/downcase.rs +++ b/crates/nu-command/src/strings/str_/case/downcase.rs @@ -121,7 +121,7 @@ fn operate( fn action(input: &Value, head: Span) -> Value { match input { - Value::String { val, .. } => Value::string(val.to_ascii_lowercase(), head), + Value::String { val, .. } => Value::string(val.to_lowercase(), head), Value::Error { .. } => input.clone(), _ => Value::error( ShellError::OnlySupportsThisInputType { diff --git a/crates/nu-command/tests/commands/str_/mod.rs b/crates/nu-command/tests/commands/str_/mod.rs index 5e136b40b8c0f..382f2bf20fe9f 100644 --- a/crates/nu-command/tests/commands/str_/mod.rs +++ b/crates/nu-command/tests/commands/str_/mod.rs @@ -76,6 +76,13 @@ fn downcases() { }) } +#[test] +fn non_ascii_downcase() { + let actual = nu!("'ὈΔΥΣΣΕΎΣ' | str downcase"); + + assert_eq!(actual.out, "ὀδυσσεύς"); +} + #[test] fn upcases() { Playground::setup("str_test_4", |dirs, sandbox| { @@ -96,6 +103,13 @@ fn upcases() { }) } +#[test] +fn non_ascii_upcase() { + let actual = nu!("'ὀδυσσεύς' | str upcase"); + + assert_eq!(actual.out, "ὈΔΥΣΣΕΎΣ"); +} + #[test] #[ignore = "Playgrounds are not supported in nu-cmd-extra"] fn camelcases() {