From fc7ed1bfe4110a45c03c5b1b64697f92c6ef7fe6 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Tue, 21 Dec 2021 11:49:02 +1100 Subject: [PATCH] switch substring to bytes (#538) * switch substring to bytes * Add a test --- crates/nu-command/src/strings/str_/substring.rs | 16 +++++++++++----- src/tests.rs | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/strings/str_/substring.rs b/crates/nu-command/src/strings/str_/substring.rs index 9e06840ab7..ec5df7ef43 100644 --- a/crates/nu-command/src/strings/str_/substring.rs +++ b/crates/nu-command/src/strings/str_/substring.rs @@ -157,12 +157,18 @@ fn action(input: &Value, options: &Substring, head: Span) -> Value { Ordering::Less => Value::String { val: { if end == isize::max_value() { - s.chars().skip(start as usize).collect::() + String::from_utf8_lossy( + &s.bytes().skip(start as usize).collect::>(), + ) + .to_string() } else { - s.chars() - .skip(start as usize) - .take((end - start) as usize) - .collect::() + String::from_utf8_lossy( + &s.bytes() + .skip(start as usize) + .take((end - start) as usize) + .collect::>(), + ) + .to_string() } }, span: head, diff --git a/src/tests.rs b/src/tests.rs index a87fed402f..fc5bf33838 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1316,3 +1316,11 @@ fn flatten_should_flatten_inner_table() -> TestResult { "123", ) } + +#[test] +fn cjk_in_substrings() -> TestResult { + run_test( + r#"let s = '[Rust 程序设计语言](title-page.md)'; let start = ($s | str index-of '('); let end = ($s | str index-of ')'); echo ($s | str substring $"($start + 1),($end)")"#, + "title-page.md", + ) +}