Fix line-end trimming in subexpression (#7543)

# Description

Currently the implementation is different for Windows and Unix.

Thus certain operations will fail if the platform foreign line ending is
used:

example failing under windows

```
git show (git merge-base main HEAD)
```

Temporary cheat is to strip all `\r` and `\n` from the end. Proper
solution should trim them as correct patterns.

Also needed: test of behavior with both platform newline and
platform-foreign line endings

cc @WindSoilder 


# User-Facing Changes

Line endings should be trimmed no matter the source and no matter the
platform

# Tests + Formatting

Still missing
This commit is contained in:
Stefan Holderbach 2023-01-08 22:51:51 +01:00 committed by GitHub
parent 5879b0df99
commit cef05d3553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,11 +6,7 @@ use crate::{
use nu_utils::{stderr_write_all_and_flush, stdout_write_all_and_flush}; use nu_utils::{stderr_write_all_and_flush, stdout_write_all_and_flush};
use std::sync::{atomic::AtomicBool, Arc}; use std::sync::{atomic::AtomicBool, Arc};
const LINE_ENDING: &str = if cfg!(target_os = "windows") { const LINE_ENDING_PATTERN: &[char] = &['\r', '\n'];
"\r\n"
} else {
"\n"
};
/// The foundational abstraction for input and output to commands /// The foundational abstraction for input and output to commands
/// ///
@ -185,7 +181,7 @@ impl PipelineData {
} }
} }
if trim_end_newline { if trim_end_newline {
output.truncate(output.trim_end_matches(LINE_ENDING).len()) output.truncate(output.trim_end_matches(LINE_ENDING_PATTERN).len())
} }
Value::String { Value::String {
val: output, val: output,
@ -279,7 +275,7 @@ impl PipelineData {
} }
} }
if trim_end_newline { if trim_end_newline {
output.truncate(output.trim_end_matches(LINE_ENDING).len()); output.truncate(output.trim_end_matches(LINE_ENDING_PATTERN).len());
} }
Ok(output) Ok(output)
} }
@ -379,7 +375,7 @@ impl PipelineData {
if let Ok(mut st) = String::from_utf8(collected.clone().item) { if let Ok(mut st) = String::from_utf8(collected.clone().item) {
if trim_end_newline { if trim_end_newline {
st.truncate(st.trim_end_matches(LINE_ENDING).len()); st.truncate(st.trim_end_matches(LINE_ENDING_PATTERN).len());
} }
Ok(f(Value::String { Ok(f(Value::String {
val: st, val: st,
@ -436,7 +432,7 @@ impl PipelineData {
if let Ok(mut st) = String::from_utf8(collected.clone().item) { if let Ok(mut st) = String::from_utf8(collected.clone().item) {
if trim_end_newline { if trim_end_newline {
st.truncate(st.trim_end_matches(LINE_ENDING).len()) st.truncate(st.trim_end_matches(LINE_ENDING_PATTERN).len())
} }
Ok(f(Value::String { Ok(f(Value::String {
val: st, val: st,
@ -488,7 +484,7 @@ impl PipelineData {
if let Ok(mut st) = String::from_utf8(collected.clone().item) { if let Ok(mut st) = String::from_utf8(collected.clone().item) {
if trim_end_newline { if trim_end_newline {
st.truncate(st.trim_end_matches(LINE_ENDING).len()) st.truncate(st.trim_end_matches(LINE_ENDING_PATTERN).len())
} }
let v = Value::String { let v = Value::String {
val: st, val: st,