wstr: Fix parsing of '+'

This commit is contained in:
Adrian Wielgosik 2024-04-12 21:50:12 +02:00 committed by Adrian Wielgosik
parent f392699f73
commit 9d2664f752
3 changed files with 67 additions and 1 deletions

View File

@ -79,6 +79,8 @@ impl<'gc> TObject<'gc> for ScriptObject<'gc> {
}
fn maybe_int_property(name: AvmString<'_>) -> DynamicKey<'_> {
// TODO: this should use a custom implementation, not parse()
// FP is much stricter here, only allowing pure natural numbers without sign or leading zeros
if let Ok(val) = name.parse::<u32>() {
DynamicKey::Uint(val)
} else {

View File

@ -106,6 +106,11 @@ mod int_parse {
None => return None,
};
// "-", "+"
if digits.is_empty() {
return None;
}
if is_neg && !T::SIGNED {
return None;
}

View File

@ -5,7 +5,7 @@ use alloc::vec::Vec;
use core::fmt::Debug;
macro_rules! bstr {
($str:literal) => {
($str:expr) => {
WStr::from_units($str)
};
}
@ -351,3 +351,62 @@ fn utf8_index_mapping_empty() {
assert_eq!(to_utf8.utf16_index(0), Some(0));
assert_eq!(to_utf8.utf16_index(1), None);
}
#[test]
fn parse() {
fn test_u32(string: &[u8]) {
let actual = bstr!(string).parse::<u32>();
if let Ok(expected) = core::str::from_utf8(string).unwrap().parse::<u32>() {
assert!(actual.is_ok());
assert_eq!(actual.unwrap(), expected);
} else {
assert!(actual.is_err());
}
}
fn test_i32(string: &[u8]) {
let actual = bstr!(string).parse::<i32>();
if let Ok(expected) = core::str::from_utf8(string).unwrap().parse::<i32>() {
assert!(actual.is_ok());
assert_eq!(actual.unwrap(), expected);
} else {
assert!(actual.is_err());
}
}
test_u32(b"0");
test_u32(b"123");
test_u32(b"001");
test_u32(b"123asd");
test_u32(b"asdf");
test_u32(b"");
test_u32(b" ");
test_u32(b"123");
test_u32(b"4294967295");
test_u32(b"4294967296");
test_u32(b"4294967297");
test_u32(b"+0");
test_u32(b"+1");
test_u32(b"-1");
test_u32(b"-0");
test_u32(b"-");
test_u32(b"+");
test_i32(b"0");
test_i32(b"123");
test_i32(b"001");
test_i32(b"123asd");
test_i32(b"asdf");
test_i32(b"");
test_i32(b" ");
test_i32(b"123");
test_i32(b"4294967295");
test_i32(b"4294967296");
test_i32(b"4294967297");
test_i32(b"+0");
test_i32(b"+1");
test_i32(b"-1");
test_i32(b"-0");
test_i32(b"-");
test_i32(b"+");
}