Skip to content

expr: Use chars().count() as we can have some multibytes chars #3133

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

Merged
merged 1 commit into from
Feb 13, 2022
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
6 changes: 4 additions & 2 deletions src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! * `<https://en.wikipedia.org/wiki/Shunting-yard_algorithm>`
//!

// spell-checker:ignore (ToDO) binop binops ints paren prec
// spell-checker:ignore (ToDO) binop binops ints paren prec multibytes

use num_bigint::BigInt;
use num_traits::{One, Zero};
Expand Down Expand Up @@ -465,7 +465,9 @@ fn operator_match(values: &[String]) -> Result<String, String> {

fn prefix_operator_length(values: &[String]) -> String {
assert!(values.len() == 1);
values[0].len().to_string()
// Use chars().count() as we can have some multibytes chars
// See https://github.com/uutils/coreutils/issues/3132
values[0].chars().count().to_string()
}

fn prefix_operator_index(values: &[String]) -> String {
Expand Down
23 changes: 23 additions & 0 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// spell-checker:ignore αbcdef

use crate::common::util::*;

#[test]
Expand Down Expand Up @@ -95,6 +97,27 @@ fn test_and() {
new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n");
}

#[test]
fn test_length_fail() {
new_ucmd!().args(&["length", "αbcdef", "1"]).fails();
}

#[test]
fn test_length() {
new_ucmd!()
.args(&["length", "abcdef"])
.succeeds()
.stdout_only("6\n");
}

#[test]
fn test_length_mb() {
new_ucmd!()
.args(&["length", "αbcdef"])
.succeeds()
.stdout_only("6\n");
}

#[test]
fn test_substr() {
new_ucmd!()
Expand Down