mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
@@ -51,7 +51,10 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
|
||||
if len == 0 {
|
||||
return Err("empty string".to_owned());
|
||||
}
|
||||
let slice = &string[..len - 1];
|
||||
let slice = match string.get(..len - 1) {
|
||||
Some(s) => s,
|
||||
None => return Err(format!("invalid time interval {}", string.quote())),
|
||||
};
|
||||
let (numstr, times) = match string.chars().next_back().unwrap() {
|
||||
's' => (slice, 1),
|
||||
'm' => (slice, 60),
|
||||
@@ -112,6 +115,11 @@ mod tests {
|
||||
assert!(from_str("123X").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_multi_bytes_characters() {
|
||||
assert!(from_str("10€").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_invalid_magnitude() {
|
||||
assert!(from_str("12abc3s").is_err());
|
||||
|
||||
Reference in New Issue
Block a user