mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
Avoid subtract overflow when no ending index given. (#2764)
This commit is contained in:
parent
5fb3df4054
commit
c857e18c4a
@ -159,12 +159,14 @@ fn action(input: &Value, options: &Substring, tag: impl Into<Tag>) -> Result<Val
|
|||||||
"End must be greater than or equal to Start",
|
"End must be greater than or equal to Start",
|
||||||
tag.span,
|
tag.span,
|
||||||
)),
|
)),
|
||||||
Ordering::Less => Ok(UntaggedValue::string(
|
Ordering::Less => Ok(UntaggedValue::string(if end == isize::max_value() {
|
||||||
|
s.chars().skip(start as usize).collect::<String>()
|
||||||
|
} else {
|
||||||
s.chars()
|
s.chars()
|
||||||
.skip(start as usize)
|
.skip(start as usize)
|
||||||
.take((end - start) as usize)
|
.take((end - start) as usize)
|
||||||
.collect::<String>(),
|
.collect::<String>()
|
||||||
)
|
})
|
||||||
.into_value(tag)),
|
.into_value(tag)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -330,6 +332,9 @@ mod tests {
|
|||||||
expectation("and", (0, -3)),
|
expectation("and", (0, -3)),
|
||||||
expectation("andr", (0, -2)),
|
expectation("andr", (0, -2)),
|
||||||
expectation("andre", (0, -1)),
|
expectation("andre", (0, -1)),
|
||||||
|
// str substring [ -4 , _ ]
|
||||||
|
// str substring -4 ,
|
||||||
|
expectation("dres", (-4, isize::max_value())),
|
||||||
expectation("", (0, -110)),
|
expectation("", (0, -110)),
|
||||||
expectation("", (6, 0)),
|
expectation("", (6, 0)),
|
||||||
expectation("", (6, -1)),
|
expectation("", (6, -1)),
|
||||||
|
Loading…
Reference in New Issue
Block a user