remove panic from lpad and rpad, change truncation behaviour for lpad (#6495)

* condense `lpad` and `rpad` into `pad`

* change description

* back to original names, add change
This commit is contained in:
pwygab 2022-09-08 20:29:56 +08:00 committed by GitHub
parent b398448cd9
commit d1e1d0ac3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View File

@ -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::<String>(),
val: val
.chars()
.rev()
.take(s)
.collect::<String>()
.chars()
.rev()
.collect::<String>(),
span: head,
}
} else {

View File

@ -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| {