Skip to content

Commit 351a56d

Browse files
Merge pull request #3133 from sylvestre/issue-3132
expr: Use chars().count() as we can have some multibytes chars
2 parents 1167d81 + 7225fb6 commit 351a56d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/uu/expr/src/syntax_tree.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! * `<https://en.wikipedia.org/wiki/Shunting-yard_algorithm>`
1111
//!
1212
13-
// spell-checker:ignore (ToDO) binop binops ints paren prec
13+
// spell-checker:ignore (ToDO) binop binops ints paren prec multibytes
1414

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

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

471473
fn prefix_operator_index(values: &[String]) -> String {

tests/by-util/test_expr.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// spell-checker:ignore αbcdef
2+
13
use crate::common::util::*;
24

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

100+
#[test]
101+
fn test_length_fail() {
102+
new_ucmd!().args(&["length", "αbcdef", "1"]).fails();
103+
}
104+
105+
#[test]
106+
fn test_length() {
107+
new_ucmd!()
108+
.args(&["length", "abcdef"])
109+
.succeeds()
110+
.stdout_only("6\n");
111+
}
112+
113+
#[test]
114+
fn test_length_mb() {
115+
new_ucmd!()
116+
.args(&["length", "αbcdef"])
117+
.succeeds()
118+
.stdout_only("6\n");
119+
}
120+
98121
#[test]
99122
fn test_substr() {
100123
new_ucmd!()

0 commit comments

Comments
 (0)