Skip to content

Commit

Permalink
use to_lowercase in str downcase (#10850)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
amtoine committed Oct 27, 2023
1 parent 7ac5a01 commit 01d8961
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/nu-command/src/strings/str_/case/downcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions crates/nu-command/tests/commands/str_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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() {
Expand Down

0 comments on commit 01d8961

Please sign in to comment.