diff --git a/crates/nu-command/src/strings/str_/lpad.rs b/crates/nu-command/src/strings/str_/lpad.rs index 374c02dd94..026fb55255 100644 --- a/crates/nu-command/src/strings/str_/lpad.rs +++ b/crates/nu-command/src/strings/str_/lpad.rs @@ -74,10 +74,10 @@ impl Command for SubCommand { }), }, Example { - description: "Use lpad to truncate a string", + description: "Use lpad to truncate a string to its last three characters", example: "'123456789' | str lpad -l 3 -c '0'", result: Some(Value::String { - val: "123".to_string(), + val: "789".to_string(), span: Span::test_data(), }), }, @@ -105,6 +105,13 @@ fn operate( column_paths: call.rest(engine_state, stack, 0)?, }); + if options.length.expect("this exists") < 0 { + return Err(ShellError::UnsupportedInput( + String::from("The length of the string cannot be negative"), + call.head, + )); + } + let head = call.head; input.map( move |v| { @@ -142,7 +149,14 @@ fn action( let s = *x as usize; if s < val.len() { Value::String { - val: val.chars().take(s).collect::(), + val: val + .chars() + .rev() + .take(s) + .collect::() + .chars() + .rev() + .collect::(), span: head, } } else { diff --git a/crates/nu-command/src/strings/str_/rpad.rs b/crates/nu-command/src/strings/str_/rpad.rs index be5061cb5f..2e9f5e463d 100644 --- a/crates/nu-command/src/strings/str_/rpad.rs +++ b/crates/nu-command/src/strings/str_/rpad.rs @@ -74,7 +74,7 @@ impl Command for SubCommand { }), }, Example { - description: "Use rpad to truncate a string", + description: "Use rpad to truncate a string to its first three characters", example: "'123456789' | str rpad -l 3 -c '0'", result: Some(Value::String { val: "123".to_string(), @@ -105,6 +105,13 @@ fn operate( column_paths: call.rest(engine_state, stack, 0)?, }); + if options.length.expect("this exists") < 0 { + return Err(ShellError::UnsupportedInput( + String::from("The length of the string cannot be negative"), + call.head, + )); + } + let head = call.head; input.map( move |v| {