Fix pad functions.

This commit is contained in:
Stephen Chung 2020-06-30 18:34:32 +08:00
parent d6a08be223
commit 52ef223f83
2 changed files with 23 additions and 19 deletions

View File

@ -41,11 +41,13 @@ fn pad<T: Variant + Clone>(engine: &Engine, args: &mut [&mut Dynamic]) -> FuncRe
} }
} }
if len >= 0 { if len > 0 {
let item = args[2].clone(); let item = args[2].clone();
let list = args[0].downcast_mut::<Array>().unwrap(); let list = args[0].downcast_mut::<Array>().unwrap();
list.resize(len as usize, item); if len as usize > list.len() {
list.resize(len as usize, item);
}
} }
Ok(()) Ok(())
} }

View File

@ -242,29 +242,31 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
} }
} }
let ch = *args[2].downcast_ref::< char>().unwrap(); if len > 0 {
let s = args[0].downcast_mut::<ImmutableString>().unwrap(); let ch = *args[2].downcast_ref::< char>().unwrap();
let s = args[0].downcast_mut::<ImmutableString>().unwrap();
let orig_len = s.chars().count(); let orig_len = s.chars().count();
if orig_len < len as usize { if len as usize > orig_len {
let p = s.make_mut(); let p = s.make_mut();
for _ in 0..(len as usize - orig_len) { for _ in 0..(len as usize - orig_len) {
p.push(ch); p.push(ch);
}
if engine.max_string_size > 0 && s.len() > engine.max_string_size {
return Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
engine.max_string_size,
s.len(),
Position::none(),
)));
}
} }
} }
if engine.max_string_size > 0 && s.len() > engine.max_string_size { Ok(())
Err(Box::new(EvalAltResult::ErrorDataTooLarge(
"Length of string".to_string(),
engine.max_string_size,
s.len(),
Position::none(),
)))
} else {
Ok(())
}
}, },
); );
lib.set_fn_3_mut( lib.set_fn_3_mut(