switch substring to bytes (#538)

* switch substring to bytes

* Add a test
This commit is contained in:
JT 2021-12-21 11:49:02 +11:00 committed by GitHub
parent 1609101e62
commit fc7ed1bfe4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View file

@ -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>()
String::from_utf8_lossy(
&s.bytes().skip(start as usize).collect::<Vec<_>>(),
)
.to_string()
} else {
s.chars()
.skip(start as usize)
.take((end - start) as usize)
.collect::<String>()
String::from_utf8_lossy(
&s.bytes()
.skip(start as usize)
.take((end - start) as usize)
.collect::<Vec<_>>(),
)
.to_string()
}
},
span: head,

View file

@ -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",
)
}